package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.Admin;
import edu.school.entity.User;
import edu.school.service.AdminService;
import edu.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Controller
public class AdminController {
    @Autowired
    AdminService service;

    //我的个人信息查询
    @RequestMapping(value = "/admin/myinfo")
    public String query( Model model, HttpSession session){
        Admin admin= (Admin) session.getAttribute("admin");
        if(admin!=null){
            Admin  record=service.findById(admin.getId());
            model.addAttribute("record",record);
        }
        return "update_admin";
    }
    @RequestMapping(value = "/admin/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Admin record, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (service.update(record)) {
                result.put("success", true);
                result.put("message", "修改成功");
                result.put("redirectUrl", "/admin/myinfo");
            } else {
                result.put("success", false);
                result.put("message", "修改失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统错误，请稍后重试");
        }
        return result;
    }
}
